package example;

import com.dodol.builder.TestCaseBuilder;
import com.dodol.common.BaseTestCase;
import com.dodol.common.TestStep;
import com.dodol.runner.TestCaseRunner;
import com.dodol.testcases.BaseTestObject;
import com.dodol.util.ExcelUtil;
import com.dodol.util.ObjectUtil;
import jxl.read.biff.BiffException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: 80736
 * @Date: 2018/9/3 08:39
 * @Description:
 */

public class DataProviderTestCase extends BaseTestObject {

    private WebDriver webDriver;

//    @BeforeMethod
//    public void setUp() {
//        webDriver = new ChromeDriver();
//        webDriver.manage().window().maximize();
//        // 页面和元素超时时间
//        webDriver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
//        webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//    }
//
//    @AfterMethod
//    public void tearDown() {
//        webDriver.close();
//    }


    @DataProvider(name = "testdata")
    public Object[][] getTestCases() throws IOException, BiffException {

        WebDriver webDriver = getWebDriver();
        List<String[]> data = ExcelUtil.generateTestData("G:\\Code\\java\\seleniumTest\\testcase.xls");
        ArrayList<BaseTestCase> testcases = TestCaseBuilder.generateTestCases(webDriver, data);
        return ObjectUtil.convertObject(testcases);
    }

    /**
     * 在一个test中执行
     *
     * @param object
     * @throws InterruptedException
     */
    @Test(dataProvider = "testdata", enabled = false)
    public void testInOneMethod(Object object) throws InterruptedException {
        List<BaseTestCase> testCases = (List<BaseTestCase>) object;
        for (BaseTestCase testcase : testCases) {
            TestCaseRunner runner = new TestCaseRunner(testcase, webDriver);
            runner.run();
        }
    }

    /**
     * 多个步骤执行
     *
     * @param testCase
     * @throws InterruptedException
     */
    @Test(dataProvider = "testdata", description = "测试百度")
    public void testInMutilStep(BaseTestCase testCase) throws InterruptedException {
        WebDriver webDriver = getWebDriver();
        BaseTestCase finalResultTestCase = ObjectUtil.repairBaseTestCaseObject(testCase, webDriver);
        TestCaseRunner runner = new TestCaseRunner(testCase, webDriver);
        runner.run();
    }

}
